package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gakki
 * @create 2022/5/2 15:26
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分类管理分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page> queryByPage(Integer page, Integer pageSize) {
        log.info("分类管理分页查询开始，page={}，pageSize={},name={}", page, pageSize);
        Page pageInfo = new Page(page, pageSize);
        categoryService.page(pageInfo);
        return Result.success(pageInfo);
    }

    /**
     * 新增category
     * @param category
     * @return
     */
    @PostMapping
    public Result<String> addDishCategory(@RequestBody Category category) {
        log.info("新增category:{}", category.toString());
        categoryService.save(category);
        return Result.success("新增菜品分类成功！");
    }

    /**
     * 删除category
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<String> deleteCategory(Long id) {
        log.info("删除category，id：{}", id);
        categoryService.deleteCategory(id);
        return Result.success("删除category成功！");
    }

    /**
     * 修改category
     * @param category
     * @return
     */
    @PutMapping
    public Result<String> modifyCategory(@RequestBody Category category) {
        log.info("更新category：{}", category);
        categoryService.updateById(category);
        return Result.success("更新category成功");
    }

    /**
     * 菜品分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> dishList(Category category) {
        log.info("菜品分类type：{}", category.getType());
        /*//判断缓存中是否有数据
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<Category> categoryList = (List<Category>) valueOperations.get("category");
        //如果有数据则直接返回
        if (categoryList != null) {
            return Result.success(categoryList);
        }else {
            //如果没有数据，则从数据库进行加载
            LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
            //排序
            lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
            List<Category> list = categoryService.list(lambdaQueryWrapper);
            //如果数据不为null，则写入缓存中
            if (list != null) {
                valueOperations.set("category", list);
            }
            //如果数据为null，则直接返回
            return Result.success(list);
        }*/
        //判断缓存中是否有数据

        //如果有数据则直接返回
            //如果没有数据，则从数据库进行加载
            LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
            //排序
            lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
            List<Category> list = categoryService.list(lambdaQueryWrapper);
            //如果数据不为null，则写入缓存中
            //如果数据为null，则直接返回
            return Result.success(list);

    }
}

